//package com.sky.demo.file.zip;
//
//import cn.hutool.core.date.DateField;
//import cn.hutool.core.date.DateUtil;
//import com.sun.deploy.util.StringUtils;
//
//import java.io.*;
//import java.nio.charset.StandardCharsets;
//import java.util.Date;
//import java.util.zip.ZipEntry;
//import java.util.zip.ZipOutputStream;
//
//public class FileUtil {
//
//
//    private static final String DEFAULT_FILE_PREFIX = "BVS_COUP_CHECK";
//    private static final String DEFAULT_FILE_DATE_FORMAT = "YYYYMMDD";
//    private static final String DEFAULT_FILE_SUFFIX = ".txt";
//    private static final String DEFAULT_FILE_NAME_SEPARATOR = "_";
//
//
//
//    //LINUX
//    private static final String domain = "http://127.0.0.1:22";
//    private static final String path = "/test";
//    private static final String filePrefix = "/file";
//
//
//
//
//
//
//    public static String getFileName(String partnerCode){
//        return DEFAULT_FILE_PREFIX + DEFAULT_FILE_NAME_SEPARATOR
//                + partnerCode + DEFAULT_FILE_NAME_SEPARATOR
//                + DateUtil.format(DateUtil.offset(new Date(), DateField.DAY_OF_MONTH, 1), DEFAULT_FILE_DATE_FORMAT) + DEFAULT_FILE_NAME_SEPARATOR
//                + DEFAULT_FILE_SUFFIX;
//    }
//
//    public static void main(String[] args) throws Exception {
//        zipData2();
//    }
//
//    public static void zipData() throws Exception {
//        String textPath = "D:\\test\\2.text";
//        String zipPath = "D:\\test\\2.zip";
//        String testText = "test";
//
//
//        ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(zipPath));
//        FileWriter fileWriter = null;
//
//        try{
//            fileWriter = new FileWriter(textPath);
//            fileWriter.write(testText);
//            fileWriter.flush();
//            fileWriter.close();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//
//        FileInputStream fis = new FileInputStream(textPath);
//        zipOutputStream.putNextEntry(new ZipEntry("request.json"));
//        int len;
//        byte[] buffer = new byte[1024];
//        while((len = fis.read(buffer)) > 0){
//            zipOutputStream.write(buffer, 0, len);
//        }
//
//        byte[] b = new byte[1024];
//        int a = fis.read(b);
//        zipOutputStream.closeEntry();
//        fis.close();
//        zipOutputStream.flush();
//        zipOutputStream.close();
//    }
//
//
//    public static void zipData2() throws Exception {
//        String textPath  = "D:\\test\\3.zip";
//        String str1 = "zip";
//        String str2 = "signature";
//
//        String name1 = "data.txt";
//        String signature = "signature.txt";
//
//
//        ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(textPath));
//        zipOutputStream.putNextEntry(new ZipEntry(name1));
//        byte[] bytes = str1.getBytes(StandardCharsets.UTF_8);
//        zipOutputStream.write(bytes, 0, bytes.length);
//        zipOutputStream.closeEntry();
//
//
//        zipOutputStream.putNextEntry(new ZipEntry(signature));
//        byte[] byte1 = str2.getBytes(StandardCharsets.UTF_8);
//        zipOutputStream.write(byte1, 0, byte1.length);
//        zipOutputStream.closeEntry();
//
//        zipOutputStream.flush();
//        zipOutputStream.close();
//    }
//
//
//    //下载zip
//    public String download(String zipUrl, HttpServletResponse response) throws UnsupportedEncodingException {
//        if (zipUrl != null) {
//            File file = new File(zipUrl);
//            // 如果文件名存在，则进行下载
//            if (file.exists()) {
//                /*设置response 的 header*/
//                // 配置文件下载
//                response.setHeader("content-type", "application/zip");
//                response.setContentType("application/zip");
//                // 下载文件能正常显示中文
//                response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(zipUrl.substring(zipUrl.lastIndexOf("/")+1), "UTF-8"));
//
//                byte[] buffer = new byte[1024];
//                FileInputStream fis = null;
//                BufferedInputStream bis = null;
//                try {
//                    fis = new FileInputStream(file);
//                    bis = new BufferedInputStream(fis);
//                    OutputStream os = response.getOutputStream();
//                    int i = bis.read(buffer);
//                    while (i != -1) {
//                        os.write(buffer, 0, i);
//                        i = bis.read(buffer);
//                    }
//                    System.out.println("Download the song successfully!");
//                }
//                catch (Exception e) {
//                    System.out.println("Download the song failed!");
//                }finally {
//                    if (bis != null) {
//                        try {
//                            bis.close();
//                        } catch (IOException e) {
//                            e.printStackTrace();
//                        }
//                    }
//                    if (fis != null) {
//                        try {
//                            fis.close();
//                        } catch (IOException e) {
//                            e.printStackTrace();
//                        }
//                    }
//                }
//            }
//        }
//        return null;
//    }
//
//}
package com.sky.demo.file.zip;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;


import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class FileUtil {


    private static final String DEFAULT_FILE_PREFIX = "BVS_COUP_CHECK";
    private static final String DEFAULT_FILE_DATE_FORMAT = "YYYYMMDD";
    private static final String DEFAULT_FILE_SUFFIX = ".txt";
    private static final String DEFAULT_FILE_NAME_SEPARATOR = "_";



    //LINUX
    private static final String domain = "http://127.0.0.1:22";
    private static final String path = "/test";
    private static final String filePrefix = "/file";






    public static String getFileName(String partnerCode){
        return DEFAULT_FILE_PREFIX + DEFAULT_FILE_NAME_SEPARATOR
                + partnerCode + DEFAULT_FILE_NAME_SEPARATOR
                + DateUtil.format(DateUtil.offset(new Date(), DateField.DAY_OF_MONTH, 1), DEFAULT_FILE_DATE_FORMAT) + DEFAULT_FILE_NAME_SEPARATOR
                + DEFAULT_FILE_SUFFIX;
    }

    public static void main(String[] args) throws Exception {
        zipData2();
    }

    public static void zipData() throws Exception {
        String textPath = "D:\\test\\2.text";
        String zipPath = "D:\\test\\2.zip";
        String testText = "test";


        ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(zipPath));
        FileWriter fileWriter = null;

        try{
            fileWriter = new FileWriter(textPath);
            fileWriter.write(testText);
            fileWriter.flush();
            fileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        FileInputStream fis = new FileInputStream(textPath);
        zipOutputStream.putNextEntry(new ZipEntry("request.json"));
        int len;
        byte[] buffer = new byte[1024];
        while((len = fis.read(buffer)) > 0){
            zipOutputStream.write(buffer, 0, len);
        }

        byte[] b = new byte[1024];
        int a = fis.read(b);
        zipOutputStream.closeEntry();
        fis.close();
        zipOutputStream.flush();
        zipOutputStream.close();
    }


    public static void zipData2() throws Exception {
        String textPath  = "http://8.142.7.101:22/test/";
        String str1 = "zip";
        String str2 = "signature";

        String name1 = "data.txt";
        String signature = "signature.txt";

        ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(textPath));
        zipOutputStream.putNextEntry(new ZipEntry(name1));
        byte[] bytes = str1.getBytes(StandardCharsets.UTF_8);
        zipOutputStream.write(bytes, 0, bytes.length);
        zipOutputStream.closeEntry();


        zipOutputStream.putNextEntry(new ZipEntry(signature));
        byte[] byte1 = str2.getBytes(StandardCharsets.UTF_8);
        zipOutputStream.write(byte1, 0, byte1.length);
        zipOutputStream.closeEntry();

        zipOutputStream.flush();
        zipOutputStream.close();
    }
}
